package com.wan37.logic.chat.dao.impl;

import com.wan37.logic.chat.dao.PrivateChatDao;
import com.wan37.logic.chat.domain.entity.PrivateChatDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.print.DocFlavor;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-27 15:08
 **/
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false, rollbackFor = Exception.class)
@Repository
public class PrivateChatDaoImpl implements PrivateChatDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;

    @Override
    public boolean add(PrivateChatDO privateChatDO) {
        try {
            hibernateTemplate.save(privateChatDO);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer id) {
        try {
            hibernateTemplate.delete(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean update(PrivateChatDO privateChatDO) {
        try {
            hibernateTemplate.update(privateChatDO);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public List<PrivateChatDO> getAll() {
        return (List<PrivateChatDO>) hibernateTemplate.find("from PrivateChatDO ");
    }
    @Override
    public List<PrivateChatDO> getPrivateChatDOBySenderId(String senderId){
        return (List<PrivateChatDO>) hibernateTemplate.find("from PrivateChatDO where senderId = ?", senderId);
    }

    @Override
    public List<PrivateChatDO> getPrivateChatDOByReceiverId(String receiverId){
        return (List<PrivateChatDO>) hibernateTemplate.find("from PrivateChatDO where receiverId = ?", receiverId);
    }

    @Override
    public List<PrivateChatDO> getPrivateChatDOBySenderIdAndReceiverId(String senderId, String receiverId) {
        return (List<PrivateChatDO>) hibernateTemplate.find
                ("from PrivateChatDO where senderId = ? and receiverId = ?", senderId, receiverId);
    }
}
